package com.ruyuan.commodity.consumer.core.consumer.goods;

import cn.hutool.json.JSONUtil;
import com.ruyuan.commodity.consumer.core.event.GoodsChangeEvent;
import com.ruyuan.commodity.consumer.service.PageCreatorService;
import com.ruyuan.commodity.open.api.model.base.message.GoodsChangeMsg;
import com.ruyuan.commodity.open.api.model.pagecreate.PageCreatePrefixEnum;
import com.ruyuan.commodity.open.api.model.payment.enums.ClientType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 商品页面生成
 *
 * @author zh
 * @version v1.0
 * @since v6.4.0 2017年8月29日 下午3:40:14
 */
@Component
public class GoodsChangeConsumer implements GoodsChangeEvent {

    public static final Logger LOG =  LoggerFactory.getLogger(GoodsChangeConsumer.class);

    @Autowired
    private PageCreatorService pageCreatorService;


    /**
     * 生成商品静态页
     *
     * @param goodsChangeMsg 商品变化对象
     */
    @Override
    public void goodsChange(GoodsChangeMsg goodsChangeMsg) {
        LOG.info("进入页面静态化的逻辑 入参为 [{}]", JSONUtil.toJsonPrettyStr(goodsChangeMsg));
        try {
            Long[] goodsIds = goodsChangeMsg.getGoodsIds();
            for (Long goodsId : goodsIds) {
                String pageName = PageCreatePrefixEnum.GOODS.getHandlerGoods(goodsId);
                LOG.info("静态化页面 pageName=[{}] ", pageName);
                // 生成静态页面
                pageCreatorService.createOne(pageName, ClientType.PC.name(), "/" + ClientType.PC.name() + pageName);
                LOG.info("静态化[{}]页面 完成", pageName);
            }
        } catch (Exception e) {
            LOG.error("静态化时出错", e);
            e.printStackTrace();
        }
    }
}
